<%@ page language="java" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
	<head>
		<%@ include file="/view/common/header.jsp"%> 
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>模型管理</title>
		<script type="text/javascript">
		function formatImg(value,row,index){
			return '<img height="20" width="20" src='+value+'>';
		}
			//查询
			function search(){
				$('#dg').datagrid('load',{
					    brand_name:   $("#brand_name").val() 
				});
			}
			//打开新增对话框
			function openAddDialog( title  , url ){
				$("#brandSave").linkbutton("enable");
				$("#dlg").dialog("open").dialog("setTitle",title);
				resetValue();
				$("#uploadPic").css("display","");
				$("#picC").css("display","none");
				window.submit_url  =  url ;
			}
			//弹出修改对话框
			function openModifyDialog( title ,url ){
				
				resetValue();
				var selectedRows=$("#dg").datagrid('getSelections');
				if(selectedRows.length>1){
					$.messager.alert("系统提示","请勿选择多条需要编辑的数据！");
					return;
				}
				if(selectedRows.length<1){
					$.messager.alert("系统提示","请选择需要编辑的数据！");
					return;
				}
				
				$("#brandSave").linkbutton("enable");
				var row=selectedRows[0];
				console.log(row.brand_pic);
				$("#dlg").dialog("open").dialog("setTitle", title);
				$('#fm #id').val(row.id);
				$('#fm #brand_name').val(row.brand_name);
				$("#uploadPic").css("display","none");
				$("#picC").css("display","");
				$("#imgPic").attr('src',row.brand_pic); 
			}
			
			function deletePic(){
				$("#imgPic").attr('src',''); 
				$("#picC").css("display","none");
				$("#uploadPic").css("display","");
			}
			
			function doDelete(){
				var selectedRows=$("#dg").datagrid('getSelections');
				if(selectedRows.length==0){
					$.messager.alert("系统提示","请选择要删除的数据！");
					return;
				}
				var strIds=[];
				var countIds=[];
				for(var i=0;i<selectedRows.length;i++){
					if(selectedRows[i].brand_count>0){
						countIds.push(selectedRows[i].id);
					}
					strIds.push(selectedRows[i].id);
				}
				var ids=strIds.join(",");
				var count=countIds.join(",");
				if(countIds.length>0){
					$.messager.alert("系统提示","品牌编号为："+count+"的数据关联有物料,不能被删除,请重新选择");
					return;
				}
				
				$.messager.confirm("系统提示","您确认要删除这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
					if(r){
						$.post("${pageContext.request.contextPath}/wmsBrand/doDeleteBrand.do",{ids:ids},function(result){
							if(result.code == '0000'){
								$.messager.alert("系统提示","数据删除成功！");
								$("#dg").datagrid("reload");
							}else{
								$.messager.alert("系统提示","数据删除失败！");
							}
						},"json");
					}
				});
			}
		
			//保存
			function save(){
				var id =$("#id").val();
				var fileName= $('#brand_pic').filebox('getValue'); 

			      if(''==id||''==$("#imgPic").attr('src')){
			    	  var file_typename =  fileName.substring(fileName.lastIndexOf(".") + 1);
				      if(!("jpg"==file_typename||"jpeg"==file_typename||"png"==file_typename)){
				    	  $.messager.alert("系统提示","请选择正确的图片格式");
							return false;	
				      }	
				}
			      $("#brandSave").linkbutton("disable");
				$("#fm").form("submit",{
					url:'../wmsBrand/doSaveBrand.do' ,
					

					onSubmit:function(){
						return $(this).form("validate");
					},
					success:function(result){
						console.log(result);
						$('#dg').datagrid('reload');
						
						res = JSON.parse(result);
						if(res.code == '0000'){
							$.messager.alert("系统提示","保存成功","info",function(){
								closeDialog();
							});
						}else if(res.code == '0001'){
							$.messager.alert("系统提示",res.msg ,"info",function(){
								//resetValue();
							});
							return;
						}else{
							$.messager.alert("系统提示",res.msg ,"info",function(){
							});
							return;
						}
					}
				});
			}
		</script>
	</head>
<body style="margin:1px;">
	<!-- 工具栏  -->
	<div id="tb">
		<div>
			<a href="javascript:openAddDialog('添加品牌 ' ,'../wmsBrand/doSaveBrand')" class="easyui-linkbutton" iconCls="icon-add" plain="true">新增</a>
			<a href="javascript:openModifyDialog('编辑品牌 ' ,'../wmsBrand/doSaveBrand')" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
			<a href="javascript:doDelete()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
		</div>
		<div>
			&nbsp;品牌名称：&nbsp;<input type="text" id="brand_name" size="20"  class="easyui-textbox" />&nbsp;
			<a href="javascript:search()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
		</div>
	</div>
	
	<!-- 查询表单 -->   
	<table id="dg" title="物料品牌" class="easyui-datagrid"  pageList="[100,200,500,1000]" pageSize="100" pagination="true" rownumbers="true"  fit="true"   url="../wmsBrand/queryForPage.do"  toolbar="#tb">
		 <thead>
				<tr>    
					<th field="cb" checkbox="true" align="center"></th>    
					<th data-options="field:'id',width:100" align="center">类别</th>
					<th data-options="field:'brand_name'" align="center">品牌名称</th>
					<th field="brand_pic" data-options="width:120,height:120,formatter:formatImg" >品牌图片</th>  
					<th data-options="field:'brand_count'" align="center">模型名称</th>
					<th data-options="field:'brand_count'" align="center">模型缩略图</th>
					<th data-options="field:'createtime'" align="center">创建日期</th>
					
				</tr>
		</thead>
	</table>


	<!-- 弹出对话框 -->
	<div id="dlg" class="easyui-dialog" style="width: 520px;height:640px;padding: 10px 20px" closed="true" buttons="#dlg-buttons">
		<form id="fm"   method="post" enctype="multipart/form-data">
				<input id="id" name="id" type="hidden" class="easyui-validatebox"/>




			<div class="hbox">
				 <div>
					 <div class="fitem">
						 <label>模型名称:</label>
						 <input   id="brand_name" name="brand_name" class="easyui-validatebox" style="width:200px" maxLength="10" data-options="required:true">
					 </div>
					 <div class="fitem">
						<label>类别:</label>
						<input   id="brand_name" name="brand_name" class="easyui-validatebox" style="width:200px" maxLength="10" data-options="required:true">
					</div>
					<div class="fitem">
						<label>品牌名称:</label>
						<input id="brandId" class="easyui-combobox" name="brandId" data-options="valueField:'brandId',textField:'title',url:''" />
					</div>
					<div  class="fitem">
						<label>模型缩略图:</label>
						<input name="brand_pic" id="brand_pic"  class="easyui-filebox" style="width:203px"  data-options="prompt:'请选择图片',buttonText:'选择图片'"/>
					</div>

					 <div  class="fitem">
						 <label>预览模型缩略图:</label>
						 <img height="200" width="200" id="imgPic" src=""/>
						 <a href="javascript:deletePic()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
					 </div>

					<div class="fitem">
						<label>模型平面图:</label>
						<input name="brand_pic" id="brand_pic"  class="easyui-filebox" style="width:203px"  data-options="prompt:'请选择图片',buttonText:'选择图片'"/>
					</div>

					 <div  class="fitem">
						 <label>预览模型平面图:</label>
						 <img height="200" width="200" id="imgPic" src=""/>
						 <a href="javascript:deletePic()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
					 </div>

					<div class="fitem">
						<label>价格:</label>
						<input name="price" id="price"  class="easyui-textbox" style="width:203px" />
					</div>

					<div class="fitem">
						<label>模型文件:</label>
						<input  name="originUrl" id="originUrl"  class="easyui-filebox" style="width:203px"  data-options="prompt:'请选择模型',buttonText:'选择模型'"/>
					</div>
				 </div>





			</div>


      </form>
	</div>
	
	<!-- 保存和关闭按钮 -->
	<div id="dlg-buttons">
		<a href="javascript:save()" id="brandSave" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
		<a href="javascript:closeDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
	</div>
</body>
</html>